package com.AuthServer.service.impl;

import com.AuthServer.common.Result;
import com.AuthServer.pojo.join.U_P;
import com.AuthServer.mapper.U_PMapper;
import com.AuthServer.service.U_PService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class U_PServiceImpl extends ServiceImpl<U_PMapper, U_P> implements U_PService {
    @Autowired
    private U_PMapper uPMapper;

    @Override
    public Result<?> addBatchAdmin(List<String> userIds) {
        List<U_P> uPs = new ArrayList<>(userIds.size());
        for (String userId : userIds) {
            //先做查询
            QueryWrapper<U_P> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId).eq("perm_id",2);
            U_P u_p = uPMapper.selectOne(wrapper);
            if (u_p == null) {
                //信息不存在才进行新增，存在不做任何操作
                U_P uP = new U_P();
                uP.setUserId(userId);
                uP.setPermId(2);
                uPs.add(uP);
            }
        }
        if (uPs.isEmpty()) {
            return Result.fail(117,"设置的用户均为管理员，请重新选择",null);
        }else {
            saveBatch(uPs,uPs.size());
            return Result.success("添加管理员成功");
        }
    }
}
